/**
 * url
 * @description only in browser
 */

const attrs = [
    'host', 'hostname', 'href',
    'origin', 'pathname', 'port',
    'protocol', 'search'
];

class URL {
    constructor(url = document.URL) {
        const a = document.createElement('a');
        a.href = url;

        attrs.forEach(k => {
            this[k] = a[k] || '';
        });

        // search -> query
        const query = this.query = {};
        this.search.slice(1).split('&').forEach(tmp => {
            let arr = tmp.split('=');
            let k = decodeURIComponent(arr[0] || '');
            let val = decodeURIComponent(arr[1] || '');

            query[k] = val;
        });

        return this;
    }
}

URL.parse = function(url) {
    return new URL(url);
};

export default URL;
